package com.ruoyi.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.IceResult;
import com.ruoyi.contract.service.IceResultService;
import com.ruoyi.contract.mapper.IceResultMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 姚煜峰
* @description 针对表【ICE_RESULT】的数据库操作Service实现
* @createDate 2023-07-07 11:18:05
*/
@Service
public class IceResultServiceImpl extends ServiceImpl<IceResultMapper, IceResult>
    implements IceResultService{

    @Override
    public AjaxResult insert(IceResult result) throws Exception {

        check(result);

        return save(result)?AjaxResult.success():AjaxResult.error();
    }

    @Override
    public AjaxResult update(IceResult result) throws Exception {
        check(result);
        return updateById(result)?AjaxResult.success():AjaxResult.error();
    }

    private void check(IceResult result) throws Exception {

        if(StringUtils.isEmpty(result.getResultName())){
            throw new Exception("字段名称不能为空");
        }

        if(StringUtils.isEmpty(result.getDatabaseName())){
            throw new Exception("数据库名称不能为空");
        }

        if(StringUtils.isEmpty(result.getCalUnit())){
            throw new Exception("计费单位不能为空");
        }

        QueryWrapper<IceResult> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(IceResult::getDatabaseName,result.getDatabaseName())
                .ne(StringUtils.isNotNull(result.getId()),IceResult::getId,result.getId());

        List<IceResult> results = list(queryWrapper);

        if(results.size()>0){
            throw new Exception("唯一校验失败,数据库名称已存在");
        }

    }
}




